package com.jiashn.minio.service.impl;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.jiashn.minio.domain.OpuSysFiles;
import com.jiashn.minio.domain.vo.MinioPearVo;
import com.jiashn.minio.mapper.OpuSysFilesMapper;
import com.jiashn.minio.service.UploadMultipartFileService;
import com.jiashn.minio.utils.JsonResult;
import com.jiashn.minio.utils.MinioBucketEnum;
import com.jiashn.minio.utils.MinioPearUploadUtil;
import com.jiashn.minio.utils.PearMinioHelper;
import io.minio.CreateMultipartUploadResponse;
import io.minio.ListPartsResponse;
import io.minio.ObjectWriteResponse;
import io.minio.messages.Part;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author: jiangjs
 * @description:
 * @date: 2023/10/25 16:47
 **/
@Service
public class UploadMultipartFileServiceImpl implements UploadMultipartFileService {
    @Resource
    private MinioPearUploadUtil minioPearUploadUtil;
    @Resource
    private OpuSysFilesMapper opuSysFilesMapper;

    @Override
    public JsonResult<?> createMultipartUploadUrl(Integer chunkNum, String fileName, String contentType) {
        return JsonResult.success(minioPearUploadUtil.createMultipartUploadUrl(chunkNum,fileName,contentType,MinioBucketEnum.EMAIL));
    }

    @Override
    public JsonResult<?> completeMultipart(Integer chunkNum, String fileName, String contentType,String uploadId,String fileMd5) {
        if (minioPearUploadUtil.completeMultipart(chunkNum,fileName,contentType,uploadId,MinioBucketEnum.EMAIL)){
            OpuSysFiles sysFiles = new OpuSysFiles();
            String suffix = StringUtils.isNoneBlank(fileName) ? fileName.substring(fileName.indexOf(".")+1) : "" ;
            sysFiles.setFileName(fileName)
                    .setBucketName(MinioBucketEnum.EMAIL.getBucket())
                    .setMd5Code(fileMd5).setSuffix(suffix);
            opuSysFilesMapper.insert(sysFiles);
            return JsonResult.success("上传成功");
        }
        return JsonResult.fails("上传失败");
    }
}
